package service

import (
	"video/globals"
	"video/internal/model"
	"video/internal/req"
	"video/internal/resp"
)

type VideoServiceImpl interface {
	VideoList(list *req.ReqVideoList) (*resp.VideoListResp, error)
}

type VideoService struct {
}

func NewVideoService() *VideoService {
	return &VideoService{}
}

func (v VideoService) VideoList(list *req.ReqVideoList) (*resp.VideoListResp, error) {
	var data []model.Video
	var total int64
	page := list.Page
	pageSize := list.PageSize

	if page < 1 {
		page = 1
	}
	if pageSize < 1 || pageSize > 6 {
		pageSize = 3
	}

	offset := (page - 1) * pageSize

	err := globals.DB.Model(&model.Video{}).Count(&total).Offset(offset).Where("title=?", list.Title).Where("user_id=?", list.UserId).Where("status=?", list.Status).Where("note=?", list.Note).Find(&data).Error

	return &resp.VideoListResp{
		Page:     list.Page,
		PageSize: list.PageSize,
		Total:    int(total),
		Data:     data,
	}, err

}
